package com.restaurant.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.restaurant.service.UserDao;
import com.restaurant.bean.User;
import com.restaurant.service.UserService;
import com.restaurant.service.impl.UserServiceImpl;
import com.restaurant.util.Result;
import com.restaurant.util.StringUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RegFrame extends JFrame {

	private JPanel contentPane;
	private JTextField pNOrEmail;
	private JPasswordField pas;
	private UserService service = new UserServiceImpl();
	private JTextField name_1;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegFrame frame = new RegFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RegFrame() {
		setTitle("注册");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("用户注册");
		lblNewLabel.setIcon(new ImageIcon(RegFrame.class.getResource("/images/ghost.png")));
		lblNewLabel.setFont(new Font("仿宋", Font.BOLD, 20));
		lblNewLabel.setBounds(139, 10, 156, 53);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("手机号或者邮箱");
		lblNewLabel_1.setIcon(new ImageIcon(RegFrame.class.getResource("/images/注册.png")));
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_1.setBounds(92, 108, 111, 18);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("密码");
		lblNewLabel_2.setIcon(new ImageIcon(RegFrame.class.getResource("/images/密码.png")));
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_2.setBounds(113, 151, 65, 15);
		contentPane.add(lblNewLabel_2);
		
		pNOrEmail = new JTextField();
		pNOrEmail.setBounds(213, 107, 129, 21);
		contentPane.add(pNOrEmail);
		pNOrEmail.setColumns(10);
		
		pas = new JPasswordField();
		pas.setBounds(213, 148, 129, 21);
		contentPane.add(pas);
		
		JButton btnNewButton = new JButton("注册");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RegActionPerformed(e);
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 12));
		btnNewButton.setBounds(164, 179, 93, 23);
		contentPane.add(btnNewButton);
		
		JLabel lblNewLabel_3 = new JLabel("用户名");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_3.setBounds(113, 76, 54, 15);
		contentPane.add(lblNewLabel_3);
		
		name_1 = new JTextField();
		name_1.setBounds(213, 73, 129, 21);
		contentPane.add(name_1);
		name_1.setColumns(10);
		this.setLocationRelativeTo(null);
	}
	/**
	 * 注册事件的处理
	 * @param e
	 */
	protected void RegActionPerformed(ActionEvent e) {
		String name = this.name_1.getText();
		String pNOrEmail = this.pNOrEmail.getText();
		String password = new String(this.pas.getPassword());
        Result result = service.addUser(name, pNOrEmail, password);
        if(result.getStatus() == 200) {
			this.dispose();
			new LoginFrame().setVisible(true);
		}else {//注册失败
			JOptionPane.showMessageDialog(null, result.getMsg());
		}
	}
}
